<?php

namespace web\controllers;

/**
 * Description of CacheController
 *
 * @author Administrator
 */
class CacheController extends \web\controllers\base\PageBaseController {

    protected $_hashKey = 'tokenlist';

    public function actionLoad() {
        $this->setLayout(NULL);
        $msg = '11';
        $quick = \Peng::$app->redis->get('$sid');
        var_dump($quick);
        if ($this->request->isPost()) {
            $biz = new \common\biz\TokenBiz();
            $data = $biz->getAll();

            foreach ($data as $key => $value) {
                $this->_saveRedis($value);
            }

            $msg = '成功';
        }

        return array('msg' => $msg);
    }

    protected function _saveRedis($value) {
        if (!\Peng::$app->redis->isConnected) {
            return;
        }

        $status = $value['status'];
        $token = $value['token'];
        $expire = 0;

        if (!empty($value['expire_at'])) {
            $expire = strtotime($value['expire_at']) - time();
        }

        if ('D' === $status || $expire < 0) {
            \Peng::$app->redis->hDel($this->_hashKey, $token);
            \Peng::$app->redis->delete($token);
        } else {
            \Peng::$app->redis->hSet($this->_hashKey, $token, 1);
            \Peng::$app->redis->save($token, 1, $expire);
        }
    }

}
